<?php
namespace haibao\cms\business\control\pc\channel\exclusive;

use haibao\cms\business\control\PublicController;
use haibao\cms\business\control\FeatureController;
use haibao\cms\model\data\FeaturePublish as FeatureModel;
class ExclusiveFlowListController extends \haibao\cms\business\control\pc\channel\exclusive\ExclusiveController{
	
    const FEATURE_TYPE = 1;
    const VIDEO_TYPE = 2;
    const DEFAULT_LIST_PAGE_NUM = 5;
    const INFO_TYPE_FEATURE = 3;
    const INFO_TYPE_VIDEO = 4;
    
    public function __construct($updateTime = null,$controlId,$isPublish){
        parent::__construct($updateTime,$controlId,$isPublish);
    }


    /**
     * 获取模版内容
     * @param : String $content
     */
    public function displayTemplateHtml(){
        $controlData = $this->getParameter(self::$CONTROL_DATA);
        $content = $this->getParameter(self::$CONTROL_CONTENT);
        $currentPage = $this->getParameter(self::$CURRENTPAGE);
        $pageable = $this->getParameter(self::$PAGEABLE);
        $this->assign('siteTemplateId', $this->getParameter(self::$SITE_TEMPLATE_ID));
        if($controlData['manual']){

        	$dataType = $controlData['manual']['dataType'];
        	$listType = $controlData['manual']['listType']; 
        	if($controlData['manual']['dataType'] == -1){
        		$dataType = 0;
        	}
			//$this->assign('viewName','priview');
			//$this->assign('pageUrl','/site/preview.html');
			$this->assign('viewName','publish');
			$url = \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_EXCLUSIVE);
			if($listType == 1){
				$fileName = '/featureflow/'.$dataType.'/';
				$urlType = 'featureflow';
			}else{
				$fileName = '/videoflow/'.$dataType.'/';
				$urlType = 'videoflow';
			}
			$url .= '/'.trim($fileName,'/').'/';
			$url = preg_replace('/\/channel/i','', $url);
            $dataCondition = $controlData['manual'];
            $dataCondition['currentPage'] = $currentPage;
            
            $this->getActualData($dataCondition);
            if($listType == 1){
            	$this->getFeatureCategory($dataType);
            }else{
            	$this->getVideoCategory($dataType);
            }
            
            $this->assign('exclusiveDomain', \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_EXCLUSIVE));
            $this->assign('staticHost', \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_STATIC_DOMAIN));
            $this->assign('homePage', \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HAIBAO_URL));
			$this->assign('pageUrl',$url);
			$this->assign('dataType',$dataType);
			$this->assign('urlType',$urlType);
			$path = $this->generateHtmlPath(__CLASS__,$controlData['manual']['id'].$currentPage);
            $this->generateHtml($path,$content);
            $content = $this->getHtml($path);
            $content = $this->replaceCDNLink($content);
            return $content;
        }
    }
    
    private function getActualData($dataCondition){
        
    	$finalList = array();
    	if($dataCondition['listType'] == self::FEATURE_TYPE){
    		list($dataList,$pager) = FeatureController::getFeatureByChannel((int)$dataCondition['dataType'],$dataCondition['limit'],true,$dataCondition['currentPage']);
    		$this->assign('siteType', self::DATA_TYPE_FEATURE);
    		foreach ($dataList as $key=>$list){
    			$temp = array();
    			$temp['Id'] = $list->Id;
    			$temp['Title'] = $list->FeatureDefine->FeatureName;
    			$temp['ChannelUrl'] = $list->Url;
    			$temp['ImageUrl'] = $list->Image->Filename;
    			$temp['PublishDate'] = $this->formatDate($list->ListTime->format('Y-m-d H:i:s'));
    			array_push($finalList,$temp);
    		}
    	}elseif($dataCondition['listType'] == self::VIDEO_TYPE){
    		$exclusiveObj = ExclusiveController::getExclusiveInstance();
    		list($dataList,$pager) = PublicController::getVideolist((int)$dataCondition['dataType'],$dataCondition['limit'],$dataCondition['currentPage'],$exclusiveObj->videoId);
    		
    		$this->assign('siteType', self::DATA_TYPE_VIDEO);
    		foreach ($dataList as $key=>$list){
    			$temp = array();
    			$temp['Id'] = $list->Id;
    			$temp['Title'] = $list->Title;
    			$temp['ChannelUrl'] = \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_EXCLUSIVE).'/play/'.$list->Id.'.htm';
    			$temp['ImageUrl'] = $list->PicUrl->Filename;
    			$temp['isVideoFile'] = 0;
    			if ($list->videoFile->VideoId && $list->videoFile->FilePath && $list->videoFile->Status==\haibao\cms\model\data\VideoFile::STATUS_OVER){
    			    $temp['isVideoFile'] = 1;    			    
    			}
    			$temp['PublishDate'] = $this->formatDate($list->PublishDate->format('Y-m-d H:i:s'));
    			array_push($finalList,$temp);
    		}
    	}
    
    	$this->assign('pageCount',$pager->getPageCount());
    	$this->assign('pager', $pager);
    	$this->assign('dataList', json_decode(json_encode($finalList)));
    }
    
    private function getVideoCategory($dataType){
    	//$videoCategoryList = \haibao\cms\business\VideoCategory::getAllCategory();
    	$videoCategoryList = array(
    		0=>array('Id'=>0,'CategoryName'=>'全部'),
    		28=>array('Id'=>28,'CategoryName'=>'明星撞衫咚咚锵'),
    		27=>array('Id'=>27,'CategoryName'=>'潮我看齐'),
    		26=>array('Id'=>26,'CategoryName'=>'报尚名来'),
    		29=>array('Id'=>29,'CategoryName'=>'MUSE美丽志'),
    		17=>array('Id'=>17,'CategoryName'=>'明星专访'),
    		224=>array('Id'=>224,'CategoryName'=>'测呗'),
    		225=>array('Id'=>225,'CategoryName'=>'最美的你'),
	    	226=>array('Id'=>226,'CategoryName'=>'摩范先生'),
    	);
    	if(isset($videoCategoryList[$dataType])){
    		$showCategory = $videoCategoryList[$dataType];
    		unset($videoCategoryList[$dataType]);
    	}else{
    		$showCategory = $videoCategoryList[0];
    	}
    	//ksort($videoCategoryList);
    	$this->assign('categoryList',$videoCategoryList);
    	$this->assign('showCategory',$showCategory);
	
    }
    
    private function getFeatureCategory($dataType){
    	
    	$array = self::getFeatureCategorys();
    	$array[0] = array('Id'=>0,'CategoryName'=>'全部');
    	$showCategory = $array[$dataType];
    	unset($array[$dataType]);
        $this->assign('categoryList', $array);
        $this->assign('showCategory', $showCategory);
        
    	
    }
}